package com.cabrida.livestar.star;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.FansGuardData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.view.CircleImageView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 明星端明星主页-守护排行榜
 */
public class StarGuardLvActivity extends BaseActivity {
	public static final String TAG = StarGuardLvActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private TextView tv_guard;
	private TextView tv_obtain;
	private ListView listView;
	private StarGuardLvAdapter adapter;
	private ArrayList<FansGuardData> infoList = new ArrayList<FansGuardData>();
	private String suid;
	private String obtain;
	private String numTotal;
	private View view_guard_1;
	private View view_guard_2;
	private View view_guard_3;
	private CircleImageView iv_header1;
	private CircleImageView iv_header2;
	private CircleImageView iv_header3;
	private ImageView iv_lv1;
	private ImageView iv_lv2;
	private ImageView iv_lv3;
	private TextView tv_name1;
	private TextView tv_name2;
	private TextView tv_name3;
	private TextView tv_lv1;
	private TextView tv_lv2;
	private TextView tv_lv3;
	private TextView tv_account_num1;
	private TextView tv_account_num2;
	private TextView tv_account_num3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	/** 前三数据 */
	private void setGuardL3Data() {
		tv_guard.setText(numTotal);
		tv_obtain.setText(obtain);
		view_guard_1.setVisibility(View.GONE);
		view_guard_2.setVisibility(View.GONE);
		view_guard_3.setVisibility(View.GONE);
		listView.setVisibility(View.GONE);
		iv_lv1.setImageResource(R.drawable.guard_num1);
		iv_lv2.setImageResource(R.drawable.guard_num2);
		iv_lv3.setImageResource(R.drawable.guard_num3);
		if (null!=infoList&&infoList.size()>0) {
			if (infoList.size()>=1) {
				view_guard_1.setVisibility(View.VISIBLE);
				MyUtils.loadImg(infoList.get(0).getU_avatar(), iv_header1);
				tv_name1.setText(infoList.get(0).getU_nickname());
				tv_account_num1.setText(infoList.get(0).getMoney());
			}
			if (infoList.size()>=2) {
				view_guard_2.setVisibility(View.VISIBLE);
				MyUtils.loadImg(infoList.get(1).getU_avatar(), iv_header2);
				tv_name2.setText(infoList.get(1).getU_nickname());
				tv_account_num2.setText(infoList.get(1).getMoney());
			}
			if (infoList.size()>=3) {
				view_guard_3.setVisibility(View.VISIBLE);
				MyUtils.loadImg(infoList.get(2).getU_avatar(), iv_header3);
				tv_name3.setText(infoList.get(2).getU_nickname());
				tv_account_num3.setText(infoList.get(2).getMoney());
			}
			if (!TextUtils.isEmpty(numTotal)) {
				int gn= Integer.valueOf(numTotal);
				if (infoList.size()>3&&gn>3) {
					listView.setVisibility(View.VISIBLE);
					infoList.remove(0);
					infoList.remove(1);
					infoList.remove(2);
					adapter.setData(infoList);
					adapter.notifyDataSetChanged();
				}
			}
		}
		
	}

	/** 获取明星守护排行榜数据 */
	private void getGuardData() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("suid", suid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_FANS_STAR_GUARD_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getGuardData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							obtain = JsonUtils.getString(data, "obtain", "");
							numTotal = JsonUtils.getString(data, "num", "");
							JSONArray list = JsonUtils.getJSONArray(data,
									"list", new JSONArray());
							infoList = JSONParser.getFansGuardList(list);
							setGuardL3Data();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_star_guard);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_guard = (TextView) findViewById(R.id.tv_guard);
		tv_obtain = (TextView) findViewById(R.id.tv_obtain);
		listView = (ListView) findViewById(R.id.listView);
		view_guard_1 = (View) findViewById(R.id.view_guard_1);
		view_guard_2 = (View) findViewById(R.id.view_guard_2);
		view_guard_3 = (View) findViewById(R.id.view_guard_3);
		iv_header1 = (CircleImageView) view_guard_1
				.findViewById(R.id.iv_header);
		iv_header2 = (CircleImageView) view_guard_2
				.findViewById(R.id.iv_header);
		iv_header3 = (CircleImageView) view_guard_3
				.findViewById(R.id.iv_header);
		iv_lv1 = (ImageView) view_guard_1.findViewById(R.id.iv_lv);
		iv_lv2 = (ImageView) view_guard_2.findViewById(R.id.iv_lv);
		iv_lv3 = (ImageView) view_guard_3.findViewById(R.id.iv_lv);
		tv_name1 = (TextView) view_guard_1.findViewById(R.id.tv_name);
		tv_name2 = (TextView) view_guard_2.findViewById(R.id.tv_name);
		tv_name3 = (TextView) view_guard_3.findViewById(R.id.tv_name);
		tv_lv1 = (TextView) view_guard_1.findViewById(R.id.tv_lv);
		tv_lv2 = (TextView) view_guard_2.findViewById(R.id.tv_lv);
		tv_lv3 = (TextView) view_guard_3.findViewById(R.id.tv_lv);
		tv_account_num1 = (TextView) view_guard_1
				.findViewById(R.id.tv_account_num);
		tv_account_num2 = (TextView) view_guard_2
				.findViewById(R.id.tv_account_num);
		tv_account_num3 = (TextView) view_guard_3
				.findViewById(R.id.tv_account_num);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		tv_title.setText("守护排行榜");

		adapter = new StarGuardLvAdapter(context);
		listView.setAdapter(adapter);
		suid = getIntent().getStringExtra("suid");
		getGuardData();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;

		default:
			break;
		}
	}
}
